722D - Generating Sets - CodeForces Solution


binary search data structures dfs and similar greedy strings trees *1900

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define YN(result) (result)? cout<<"YES\n" : cout<<"NO\n";
typedef long long int ll;
long long int const MOD = 1e9 + 7;
int const N = 2e5 + 7;

void solve()
{
    int n; cin >> n;
    vector <ll> v(n);
    for(auto &x : v) cin >> x;
    set <ll> ans;
    priority_queue <ll> q;
    sort(v.rbegin(), v.rend());
    for(auto x : v){
        ans.insert(x);
        q.push(x);
    }
    ll op = 0;
    while(1){
        ll a = q.top();
        ll b = a;
        int loop = 0;
        while(ans.count(a) != 0 && a > 0){
            if(a & 1) a = (a - 1) / 2;
            else a = a / 2;
            if(a == 0) break;
            if(ans.count(a) == 0) op = 0;
        }
        
        if(a > 0){
            ans.erase(b);
            ans.insert(a);
            q.push(a);
        }
        op++;
        q.pop();
        if(op == 2 * n) break;
    }
    for(auto x : ans){
        cout << x << " ";
    }
    cout << "\n";
}

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    int t = 1;
    // cin >> t;
    while(t--){
        solve();
    }
    return 0;
}
  		     	       	   		 	 		


Comments

Submit
0 Comments
More Questions

1278B - A and B
1353D - Constructing the Array
1269C - Long Beautiful Integer
1076A - Minimizing the String
913C - Party Lemonade
1313A - Fast Food Restaurant
681A - A Good Contest
1585F - Non-equal Neighbours
747A - Display Size
285A - Slightly Decreasing Permutations
515C - Drazil and Factorial
1151E - Number of Components
1151F - Sonya and Informatics
556A - Case of the Zeros and Ones
867A - Between the Offices
1569A - Balanced Substring
260A - Adding Digits
1698C - 3SUM Closure
1029B - Creating the Contest
1421A - XORwice
1029A - Many Equal Substrings
1675D - Vertical Paths
1271C - Shawarma Tent
805A - Fake NP
1163A - Eating Soup
787A - The Monster
807A - Is it rated
1096A - Find Divisible
1430C - Numbers on Whiteboard
1697B - Promo